package com.homepage.service;

import com.homepage.domain.FeaturetteImage;
import com.homepage.repository.FeaturetteImageRepository;
import com.homepage.service.dto.FeaturetteImageDTO;
import com.homepage.service.mapper.FeaturetteImageMapper;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service Implementation for managing {@link FeaturetteImage}.
 */
@Service
@Transactional
public class FeaturetteImageService {

    private final Logger log = LoggerFactory.getLogger(FeaturetteImageService.class);

    private final FeaturetteImageRepository featuretteImageRepository;

    private final FeaturetteImageMapper featuretteImageMapper;

    public FeaturetteImageService(FeaturetteImageRepository featuretteImageRepository, FeaturetteImageMapper featuretteImageMapper) {
        this.featuretteImageRepository = featuretteImageRepository;
        this.featuretteImageMapper = featuretteImageMapper;
    }

    /**
     * Save a featuretteImage.
     *
     * @param featuretteImageDTO the entity to save.
     * @return the persisted entity.
     */
    public FeaturetteImageDTO save(FeaturetteImageDTO featuretteImageDTO) {
        log.debug("Request to save FeaturetteImage : {}", featuretteImageDTO);
        FeaturetteImage featuretteImage = featuretteImageMapper.toEntity(featuretteImageDTO);
        featuretteImage = featuretteImageRepository.save(featuretteImage);
        return featuretteImageMapper.toDto(featuretteImage);
    }

    /**
     * Partially update a featuretteImage.
     *
     * @param featuretteImageDTO the entity to update partially.
     * @return the persisted entity.
     */
    public Optional<FeaturetteImageDTO> partialUpdate(FeaturetteImageDTO featuretteImageDTO) {
        log.debug("Request to partially update FeaturetteImage : {}", featuretteImageDTO);

        return featuretteImageRepository
            .findById(featuretteImageDTO.getId())
            .map(existingFeaturetteImage -> {
                featuretteImageMapper.partialUpdate(existingFeaturetteImage, featuretteImageDTO);

                return existingFeaturetteImage;
            })
            .map(featuretteImageRepository::save)
            .map(featuretteImageMapper::toDto);
    }

    /**
     * Get all the featuretteImages.
     *
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public List<FeaturetteImageDTO> findAll() {
        log.debug("Request to get all FeaturetteImages");
        return featuretteImageRepository
            .findAll()
            .stream()
            .map(featuretteImageMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one featuretteImage by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<FeaturetteImageDTO> findOne(Long id) {
        log.debug("Request to get FeaturetteImage : {}", id);
        return featuretteImageRepository.findById(id).map(featuretteImageMapper::toDto);
    }

    @Transactional(readOnly = true)
    public Optional<FeaturetteImageDTO> findOneByFeaturetteId(Long id) {
        log.debug("Request to get FeaturetteImage : {}", id);
        return featuretteImageRepository.findFeaturetteImageByFeaturetteId(id).map(featuretteImageMapper::toDto);
    }

    /**
     * Delete the featuretteImage by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete FeaturetteImage : {}", id);
        featuretteImageRepository.deleteById(id);
    }
}
